﻿using System.Collections.Generic;
using System.Windows.Controls;
using Bazookabird.Common;
using Bazookabird.Common.Plugin;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;

namespace Bazookabird.Wpf.Builds
{
    public class BuildListViewModel : PanelViewModelBase
    {
        private readonly IShell _shell;
        private readonly TfsTeamProjectCollection _projectCollection;
        private readonly string _projectName;

        public BuildListViewModel(IShell shell, TfsTeamProjectCollection projectCollection, string projectName, IEnumerable<IBuildHandler> buildHandlers)
        {
            _shell = shell;
            BuildHandlers = buildHandlers;
            _projectName = projectName;
            _projectCollection = projectCollection;
        }

        private IEnumerable<IBuildHandler> _buildHandlers;
        public IEnumerable<IBuildHandler> BuildHandlers
        {
            get { return _buildHandlers; }
            set
            {
                _buildHandlers = value;
                NotifyOfPropertyChange(() => BuildHandlers);
            }
        }

        private IBuildHandler _selectedBuildHandler;
        public IBuildHandler SelectedBuildHandler
        {
            get { return _selectedBuildHandler; }
            set
            {
                _selectedBuildHandler = value;
                NotifyOfPropertyChange(() => SelectedBuildHandler);
                NotifyOfPropertyChange(() => CanAddBuildHandler);
            }
        }

        public void AddBuildHandler()
        {
            SelectedBuildHandler.BuildDefinition = SelectedBuildDefinition;
            _shell.AddPluginToCurrentScreen(SelectedBuildHandler);
        }

        public bool CanAddBuildHandler
        {
            get { return SelectedBuildDefinition != null && SelectedBuildHandler != null; }
        }

        protected override void OnActivate()
        {
            base.OnActivate();
            DisplayName= "List of Builds";

            SetAvailableBuilds();
        }



        private BuildDefinitionWrapper _selectedBuildDefinition;
        public BuildDefinitionWrapper SelectedBuildDefinition
        {
            get { return _selectedBuildDefinition; }
            set 
            { 
                _selectedBuildDefinition = value; 
                NotifyOfPropertyChange(() => SelectedBuildDefinition);
                NotifyOfPropertyChange(() => CanAddBuildHandler);
            }
        }

        private BuildDefinitionWrapperCollection _buildCollection;
        public BuildDefinitionWrapperCollection BuildCollection
        {
            get { return _buildCollection; }
            set
            {
                _buildCollection = value;
                NotifyOfPropertyChange(() => BuildCollection);
            }
        }

        private ContextMenu _contextMenu;
        public ContextMenu ContextMenu
        {
            get { return _contextMenu; }
            set
            {
                _contextMenu = value;
                NotifyOfPropertyChange(() => ContextMenu);
            }
        }

        public void SetAvailableBuilds()
        {
            var buildServer = _projectCollection.GetService<IBuildServer>();            
            var buildDefinitions = buildServer.QueryBuildDefinitions(_projectName);
            BuildCollection = BuildDefinitionWrapperCollection.Create(buildDefinitions);
        }
    }
}
